fix(compiler,openapi): keep object value members named __proto__ - #11744
Open
om singhal (Om-singhaI) wants to merge 1 commit into
Open
fix(compiler,openapi): keep object value members named __proto__#11744om singhal (Om-singhaI) wants to merge 1 commit into
om singhal (Om-singhaI) wants to merge 1 commit into
Conversation
When the compiler marshals an object value for a decorator, objectValueToJs in the JS marshaller added each member with a plain assignment. For a member named __proto__ that assignment does not create a property; it invokes the Object.prototype.__proto__ setter. The member silently disappeared, and when its value was an object or an array that value became the prototype of the marshalled object, so the decorator could read names the .tsp author never declared as members. Nothing reported a diagnostic. The checker already stores object value members in a Map, and unmarshalJsToValue in the same file builds its Map from entries, so the marshalling loop was the only place in the compiler where the member was lost. objectValueToJs now builds the result with Object.fromEntries, which defines every member as an own data property and never consults the setter. The same pattern existed one step further along the issue's own path. convertRemainingValuesToExtensions in @typespec/openapi copies the object it receives from the compiler with Object.entries followed by assignment, so after the compiler fix it would have dropped the member again and polluted the prototype of the stored extension in the same way. That copy now goes through Object.fromEntries as well, keeping its existing behaviour of skipping undefined values. Regression tests cover both packages: the compiler tests check the object a decorator receives, and the openapi tests check what @extension stores, for a member named __proto__ holding a string and holding an object. Each test asserts the member is an own enumerable property and that the prototype of the result is still Object.prototype. Fixes microsoft#11743
|
Azure Pipelines: Successfully started running 1 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a JavaScript marshalling edge case where object-value members named __proto__ were being dropped (and could mutate the marshalled object’s prototype) when passed from the TypeSpec compiler to decorators, and when @typespec/openapi’s @extension decorator re-materialized those objects.
Changes:
- Update the compiler’s object-value marshalling to use
Object.fromEntries(...)so__proto__becomes an own data property instead of triggering the prototype setter. - Update OpenAPI’s
convertRemainingValuesToExtensionsobject branch to useObject.fromEntries(...)(preserving the existingundefined-skipping behavior) to avoid reintroducing the same issue. - Add regression tests in both
@typespec/compilerand@typespec/openapi, plus separate Chronus fix entries for each package.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/compiler/src/core/js-marshaller.ts | Switch object-value marshalling to Object.fromEntries to preserve __proto__ safely. |
| packages/openapi/src/decorators.ts | Switch extension object conversion to Object.fromEntries to avoid __proto__ setter behavior. |
| packages/compiler/test/checker/decorators.test.ts | Add compiler regression tests covering __proto__ as string/object values. |
| packages/openapi/test/decorators.test.ts | Add OpenAPI @extension regression tests covering __proto__ as string/object values. |
| .chronus/changes/fix-marshal-proto-member-2026-8-21.md | Changelog entry for the compiler fix. |
| .chronus/changes/fix-extension-proto-member-2026-8-22.md | Changelog entry for the OpenAPI fix. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix(compiler,openapi): keep object value members named
__proto__Fixes #11743
What is broken and why
When the compiler marshals an object value for a decorator,
objectValueToJsinpackages/compiler/src/core/js-marshaller.ts(lines 88 to 94 on main) added each member with a plain assignment:For a member named
__proto__that assignment does not create a property. It invokes theObject.prototype.__proto__setter instead. Two things follow:value.polluted === truewithpollutednot an own property).The checker stores object value members in a
Map(checkObjectLiteralPropertiesinchecker.ts), andunmarshalJsToValuein the same marshaller file already builds aMapfrom entries, so this loop was the only place in the compiler where the member was lost.The issue reproduces through
@extensionfrom@typespec/openapi, and that path has a second copy of the same pattern.convertRemainingValuesToExtensionsinpackages/openapi/src/decorators.ts(lines 101 to 108 on main) rebuilds the object it receives from the compiler withObject.entriesfollowed byresult[key] = .... Once the compiler hands it an object with an own__proto__property,Object.entrieslists that key and the assignment hits the setter again, so the extension stored by the decorator lost the member and had its prototype replaced in exactly the same way. Fixing only the compiler would not have changed the output of the issue's playground.The fix
Both sites now build the object with
Object.fromEntries, which defines every member as an own enumerable data property (it uses CreateDataProperty) and never consults the setter.packages/compiler/src/core/js-marshaller.ts:packages/openapi/src/decorators.ts, in the plain object branch ofconvertRemainingValuesToExtensions:The openapi copy keeps its existing behaviour of skipping
undefinedvalues, and the nested case was already handled by recursion. The result still inherits fromObject.prototypein both places.Object.create(null)was deliberately not used because it would change the prototype of every object handed to every decorator, which is a larger behaviour change than the bug warrants.Object.fromEntrieswas preferred overObject.definePropertybecause it reads like the surrounding code (unmarshalJsToValuealready uses it) and needs no descriptor flags.Change entries: one
fixentry for@typespec/compilerand a separatefixentry for@typespec/openapi, as the repository guidelines ask for one entry per package.chronus verifyreports both packages documented.Testing
Reproduction before the fix
End to end through
@extension, compiled with the openapi test host (Tester.compile) and read back withgetExtensions, using the three cases from the issue. With the compiler fix applied but@typespec/openapiuntouched, the stored extensions measured:So the compiler change alone did not reach the reporter's output. On pristine main the decorator argument itself measured the same way (
ownKeys=["ok"],prototypeOwnNames=["polluted"],arg.polluted === truefor the object case;ownKeys=["ok"]for the string case).With both fixes:
New tests
packages/compiler/test/checker/decorators.test.ts(undervalue marshalling > passing an object value):__proto__holding a string as an own property__proto__holding an object as an own propertypackages/openapi/test/decorators.test.ts(under@extension):__proto__holding a string as an own property__proto__holding an object as an own propertyEach test asserts
Object.prototype.hasOwnProperty.call(value, "__proto__"),Object.keys(value)equals["__proto__", "ok"], the member's value, thatObject.getPrototypeOf(value) === Object.prototype, and for the object case thatvalue.pollutedisundefined.Commands and counts
All runs use
npx vitest run(vitest 4.1.10) from the package directory. The openapi tests execute the compiler and the openapi decorator from theirdistbuilds, sotsc -p tsconfig.build.jsonwas run in both packages before each openapi run.packages/compiler:npx vitest runjs-marshaller.ts(file restored froma791115b)npx vitest run test/checker/decorators.test.ts__proto__tests, failing at thehasOwnPropertyassertionnpx vitest run test/checker/decorators.test.tsnpx vitest runpackages/openapi:npx vitest runsrc/decorators.tsstashed anddistrebuiltnpx vitest run test/decorators.test.ts__proto__tests, failing at thehasOwnPropertyassertionnpx vitest run test/decorators.test.tsnpx vitest runLint and formatting
prettier --checkon the four changed source files and the two change entries: clean.oxlint --deny-warningson the four changed source files: no findings.cspellon all six changed files: 0 issues.chronus verify: all changed packages documented (@typespec/compiler,@typespec/openapi).tsc -p tsconfig.build.jsoninpackages/compilerandpackages/openapi: clean.oxlint . --deny-warnings(thepnpm lintscript) inpackages/openapi: exit 0, no findings. The same oxlint invocation was checked to report adebugger;statement (exit 1), so it is actually linting.tsp compile . --warn-as-error --import @typespec/library-linter --no-emit(thelint-typespec-librarybuild step) inpackages/openapi: compilation completed successfully.Notes for reviewers
@extensionhand over. A decorator that copies its argument withObject.assign(target, arg)or afor ... inloop with assignment will trigger the setter again on its own copy, because those use[[Set]]. Object spread andObject.fromEntriesdo not, so copies made that way keep the member.serializeObjectValueAsJsoninpackages/compiler/src/lib/examples.ts(line 161, model property or encoded names), theModelcase oftypespecTypeToJsonInternalinpackages/compiler/src/core/decorator-utils.ts(line 262, model property names), and the argument record inpackages/compiler/src/core/auto-decorator.ts(line 43, decorator parameter names). A model property or parameter literally named__proto__would hit the same setter there. Happy to handle them in this PR or a follow up if maintainers prefer.--type-aware) could only be run withpackages/compiler/distmoved aside; withdistpresent it errors on a tsconfig input overlap fordist/src/server/tmlanguage.d.tsthat is unrelated to this change.pnpm lintandpnpm format:checkwere run per file rather than at the monorepo root.